const express = require('express');
const router = express.Router();
const { Category } = require('../models');
const { successResponse, failureResponse } = require('../utils/responses')
const { setKey, getKey } = require('../utils/redis');

router.get('/', async (req, res) => {
  try {
    let categories = await getKey('categories')
    if (!categories) {
      categories = await Category.findAll({
        order: [['rank', 'ASC'], ['id', 'DESC']]
      });
      setKey('categories', categories)
    }
    successResponse(res, '成功查询分类', categories);
  } catch (error) {
    failureResponse(res, error);
  }
});

module.exports = router;